package com.example.controller.web;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.common.Result;
import com.example.dto.ArticleDetailDTO;
import com.example.dto.ArticlePageDTO;
import com.example.service.ArticleService;
import com.example.vo.ArticleDetailVO4Web;
import com.example.vo.ArticlePageVO;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController("webArticleController")
@RequestMapping("/article")
public class ArticleController {
    @Resource
    private ArticleService articleService;

    @PostMapping("/detail")
    public Result<ArticleDetailVO4Web> detail(@RequestBody ArticleDetailDTO articleDetailDTO) {
        try {
            // 参数检查
            if (articleDetailDTO == null || articleDetailDTO.getArticleId() == null) {
                log.error("前端文章详情请求 - 参数错误: articleDetailDTO为空或articleId为空");
                return Result.fail("文章ID不能为空");
            }
            
            log.info("前端文章详情请求 - 文章ID: {}", articleDetailDTO.getArticleId());
            
            // 获取文章详情
            ArticleDetailVO4Web vo = articleService.getArticleDetail(articleDetailDTO);
            
            // 空值检查
            if (vo == null) {
                log.error("前端文章详情响应 - 文章不存在，ID: {}", articleDetailDTO.getArticleId());
                return Result.fail("文章不存在");
            }
            
            log.info("前端文章详情响应 - 标题: {}, 封面URL: {}, 内容长度: {}", 
                    vo.getTitle(), 
                    vo.getCover(), 
                    vo.getContent() != null ? vo.getContent().length() : 0);
            
            return Result.success(vo);
        } catch (Exception e) {
            log.error("获取文章详情异常，文章ID: {}, 错误: {}", 
                    articleDetailDTO != null ? articleDetailDTO.getArticleId() : "未知", 
                    e.getMessage(), e);
            return Result.fail("获取文章详情失败: " + e.getMessage());
        }
    }
    
    @PostMapping("/search")
    public Result<IPage<ArticlePageVO>> search(@RequestBody ArticlePageDTO articlePageDTO) {
        try {
            if (articlePageDTO == null) {
                log.error("文章搜索请求 - 参数错误: articlePageDTO为空");
                return Result.fail("搜索参数不能为空");
            }
            
            log.info("文章搜索请求 - 关键词: {}, 页码: {}, 每页数量: {}", 
                    articlePageDTO.getSearchTitle(), 
                    articlePageDTO.getCurrent(), 
                    articlePageDTO.getSize());
            
            // 调用服务层进行搜索
            IPage<ArticlePageVO> results = articleService.getPage(articlePageDTO);
            
            log.info("文章搜索响应 - 关键词: {}, 总条数: {}, 总页数: {}", 
                    articlePageDTO.getSearchTitle(), 
                    results.getTotal(), 
                    results.getPages());
            
            return Result.success(results);
        } catch (Exception e) {
            log.error("搜索文章异常，关键词: {}, 错误: {}", 
                    articlePageDTO != null ? articlePageDTO.getSearchTitle() : "未知", 
                    e.getMessage(), e);
            return Result.fail("搜索文章失败: " + e.getMessage());
        }
    }
} 